Hello everybody and welcome back to the costs.
So in the last session we learned how to check whether that condition is true or false.
And paisan returned as a true or false but so-called boolean objects so typically checking whether their
condition is true or false is only 50 percent of our job because in real world situations we first have
to check whether a condition is true and if so we have to take action one and a false.
We have to take action too.
So here's the point where we introduce if elif and s statements that are doing a very good job here
in Python.
So first of all we define two variables A and B equals five.
And then we check whether A is less than 10.
So we start with the keyword.
If if a less than 10 and then we need a colon and then we press enter and you can see it here.
Python makes automatically and indent.
And now we have to define what our accent is here.
If our statement.
Yes correct.
So obviously our statement is correct.
So our accent should be please print me a is less than 10.
So let's check what happens if we run the cell so we get A's less than 10.
That's correct.
So now what happens if we change here the comparison operator to a is greater than 10.
So obviously the statement is false.
So let's see what happens so Python prints nothing.
So the statement is not true.
So we don't have to take action one which is print A is less than 10.
So now we have to define an alternative action and then we introduce the keyword else you make a colon
on press enter and then you can see also here Python makes automatically and.
And then and then we define our alternative action.
So if the statement is greater than 10 it's not true.
What should be the action and the action here is print
a is not greater than 10.
So let's see what happens if we run the cell we get the message a is not greater than tetanus.
That's correct.
So now if you change again here the operator
is less than 10 and we execute again.
Then we get again is less than 10.
So what Python makes here internally is actually checks whether the conduit condition goes through and
then as it is true it takes the action print and then uh passes and stops.
So there are some sources for mistakes here and statements.
You need to make the colon.
Yeah.
If you forget the colon then we get an error message.
So the colon is important.
And also here the intent you have to make the indent here if you don't make it make the indent here.
You get them error message.
And typically paste and makes the indent automatically if you press here enter after the colon and you
can also by pressing the key tab.
You can make the indent here and also for the eight F statements of the US statement so you have the
same and then there's then the if statement.
So if the h s statement has another indent it also doesn't work.
So the that men should have the same end as the f statement and the axiom here in this case the print
statements should have phone and indent.
So this this year.
Correct.
All right.
So let's move to another example and we compare a and b.
So if a is greater than B which is not the case then please print a is not greater than B.
So if this statement is not true Python then checks if our alternative condition Elif.
So that's the keyword for checking an alternative condition.
So as of a is less than B but to see also not the case please print me is less than B and this is also
false.
So Python continues and then there's the s statement.
So if condition one and condition two they're both false then in all other cases please print me a C
could be a and b in this case of course Python prints a is equal to B and again but Python does internally
it checks in the first condition if it's true it takes action one and stops and if a condition one is
not true it checks a condition two and if condition two is true it takes the action of condition two
and then stops and yeah if all statements are conditions are not true then Python does the action which
is determined by the s statements or for all other cases and we see now on the next example that we
can have more than one elif statement.
So now let's assume that we are ordering some goods in an online web shop and we get some discounts
depending on our price.
And there are some discount targets.
So if the price is lower than 50 dollars we get a zero percent discount if it's between 50 and 100.
So 50 including until 100 excluding we get 10 percent discount if our prices in the entire were 100
including to 200 excluding a 20 percent discount.
And if our price is equal or greater than two hundred dollar or we get a discount of 25 percent we can
transform this into a code and say so if our price is less than our discount and Turnbull at positions
there also if our price is less than 50 you will get a discount of 0 percent and then Elif.
So if this statement is not correct but the price is less than 100 we get a discount of 10 percent.
And if also this condition is not true then we check if our price is less than 200 and also if this
is not true then we know that our price must be greater than 200 or greater equal than two hundred and
in all other cases here greater than two hundred our discount is 25 percent.
So now let's check this.
Our price is seventy five dollars and we run here.
Our if and Elif statements our offer discount price is sixty seven point five.
So we get 10 percent discount because our prices in the interval 50 to 100 where we get 10 percent discount.
And also let's assume if we have a price of 40 so I would assume to get a zero percent discount and
our after this comprises 40.
And also let's assume our prices 1000
we get an after discount prices of 750.
So we get 25 percent discount.
And we can further increase them the elif statement.
So there is no restriction on the Ellis statements.
We cannot fear let's say 50 Elif statements.
So this is quite flexible here.
All right so let's move on to the vile Lou.
So let's set eight to 10 and let's check whether a is less than 20 and if it is less than 20 we increase
a by 1.
So this this year a plus equals one is the same as a equals a plus 1.
So that's an abbreviation here.
We can do that so if a is less than 20 year please increase a by one and print a and let's see what
happens.
A fear on the sale so we get 11 and then we can rerun again because now is eleven and we can check again
if it's less than 20.
Now we got 12 and we can repeat this process many times
until we it reads uh 20 in the end.
So that s second.
So now we have reached 20 and now if you rerun the cell again we check whether a is less than 20 or
so 20 is less than 20 and that is this is a false statement.
And here our loop stops.
Yeah.
And as you can see here I mean it's a bit inconvenient to do this process or to repeat this process
manually.
And they are the while loop comes into play.
So we actually do the same task as Sierra buff but introduce the device statement.
And we define a that's 10 and then we make an hour repeated.
If statement and check whether a is less than 20 and if it's the case we increase a by 1 here and print
a.
And so now let's see what happens.
So Python automatically does um.
The repetition of our if statement.
So you know the first check is um a s 10.
So the first step is uh Python checks whether 10 is less than 20 in this case that increases a by 1
and prints a.
And now Python repeats the process so it takes a equals eleven.
Checks that it's less than 20.
That's correct.
Increases a by 1.
And again prints a lot.
There's twelve Python repeats the process until we reach a equals twenty.
And this is all automated.
So we don't have to to make this manually.
And that's the purpose of a bi loop.
And then in the end we can we can also print again a check.
Okay.
This is our final result.
So the value stopped when a is 20 for let's move onto a more moralistic case here a more real world
situation to say so let's assume we are in charge of loading a container ship and we have to check if
the freight exceeds the maximum freight that is allowed and we assume here that the maximum allowed
weight of 1000 tonnes.
And our actual weight is 1100 tons and we have many containers on our ship and we can reduce our freight
by unloading a container.
And we assume that a container is way to 70 tonnes so past that we can reduce the weight by 17 tonnes.
And so we are introducing here a while loop.
So we are checking if our weight is greater than our maximum allowed weight and if this is the case
we print the caution statement of freight with X tonnes is too heavy and then the next step we reduce
our weight uh with the by our reduction steps so we unload one container and we will put this process
so many times until our actual weight is less or is not greater than our maximum weight.
And in the end we reach it and our while loop stops we print.
Um the final result.
The freight with X tonsils.
Okay.
And um yeah let's assume that our our actual weight is 1100 tonnes.
So it's it's heavier than our max weight and let's check what pace does here.
So it first checks as if our initial weight 1100 tonnes is okay.
No it's not okay.
Then it reduces our weight by 17 tonnes.
And also 1000 eighty three tonnes is too heavy.
And it repeats the steps so many times until we reach afraid of 998 tonnes.
And here the binder stops and we get a printout that nine hundred ninety eight tonnes is okay.
So this works.
All right.
So we learned a lot about if elif and s statements and the Y loop and the next session we are getting
introduced to for loops which are also very important so see you in the next session.
Thank you.
Bye.